package com.doubao.chat.controller;

import com.doubao.common.result.Result;
import com.doubao.chat.dto.RechargeTokenRequest;
import com.doubao.user.service.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.time.LocalDate;
import java.util.Map;

/**
 * Token消费管理控制器
 */
@RestController
@RequestMapping("/token")
@RequiredArgsConstructor
@Tag(name = "Token管理", description = "Token余额和消费相关接口")
@Slf4j
public class TokenConsumptionController {

    private final TokenService tokenService;

    /**
     * 获取用户Token余额
     */
    @GetMapping("/balance")
    @Operation(summary = "获取Token余额", description = "获取当前登录用户的Token余额")
    public Result<Map<String, Object>> getTokenBalance(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId) {

        if (userId == null) {
            return Result.failed("用户未登录");
        }

        int balance = tokenService.getUserTokenBalance(userId);

        Map<String, Object> result = Map.of(
                "balance", balance,
                "date", LocalDate.now().toString()
        );

        return Result.success(result);
    }

    /**
     * 获取用户Token消费历史
     */
    @GetMapping("/history")
    @Operation(summary = "获取Token消费历史", description = "获取当前登录用户的Token消费记录")
    public Result<Object> getTokenHistory(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {

        if (userId == null) {
            return Result.failed("用户未登录");
        }

        return tokenService.getConsumptionHistory(userId, page, size);
    }

    /**
     * 获取用户Token消费统计
     */
    @GetMapping("/stats")
    @Operation(summary = "获取Token消费统计", description = "获取当前登录用户的Token消费统计数据")
    public Result<Object> getTokenStats(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Parameter(description = "开始日期") @RequestParam(defaultValue = "#{T(java.time.LocalDate).now().minusDays(30)}") String startDate,
            @Parameter(description = "结束日期") @RequestParam(defaultValue = "#{T(java.time.LocalDate).now()}") String endDate) {

        if (userId == null) {
            return Result.failed("用户未登录");
        }

        return tokenService.getConsumptionStats(userId, startDate, endDate);
    }

    /**
     * 充值Token
     */
    @PostMapping("/recharge")
    @Operation(summary = "充值Token", description = "为当前登录用户充值Token")
    public Result<Void> rechargeToken(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Valid @RequestBody RechargeTokenRequest request) {

        if (userId == null) {
            return Result.failed("用户未登录");
        }

        return tokenService.rechargeTokens(userId, request.getTokenAmount());
    }
}